/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.service.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserAlbum;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserAlbumDao;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserAlbumContent;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserAlbumContentDao;

/**
 * 用户相册Service
 * @author 罗天文
 * @version 2018-03-29
 */
@Service
@Transactional(readOnly = true)
public class HkUserAlbumService extends CrudService<HkUserAlbumDao, HkUserAlbum> {

	@Autowired
	private HkUserAlbumContentDao hkUserAlbumContentDao;
	
	public HkUserAlbum get(String id) {
		HkUserAlbum hkUserAlbum = super.get(id);
		hkUserAlbum.setHkUserAlbumContentList(hkUserAlbumContentDao.findList(new HkUserAlbumContent(hkUserAlbum)));
		return hkUserAlbum;
	}
	
	public List<HkUserAlbum> findList(HkUserAlbum hkUserAlbum) {
		return super.findList(hkUserAlbum);
	}
	
	public Page<HkUserAlbum> findPage(Page<HkUserAlbum> page, HkUserAlbum hkUserAlbum) {
		return super.findPage(page, hkUserAlbum);
	}
	
	@Transactional(readOnly = false)
	public void save(HkUserAlbum hkUserAlbum) {
		super.save(hkUserAlbum);
		for (HkUserAlbumContent hkUserAlbumContent : hkUserAlbum.getHkUserAlbumContentList()){
			if (hkUserAlbumContent.getId() == null){
				continue;
			}
			if (HkUserAlbumContent.DEL_FLAG_NORMAL.equals(hkUserAlbumContent.getDelFlag())){
				if (StringUtils.isBlank(hkUserAlbumContent.getId())){
					hkUserAlbumContent.setAlbum(hkUserAlbum);
					hkUserAlbumContent.preInsert();
					hkUserAlbumContentDao.insert(hkUserAlbumContent);
				}else{
					hkUserAlbumContent.preUpdate();
					hkUserAlbumContentDao.update(hkUserAlbumContent);
				}
			}else{
				hkUserAlbumContentDao.delete(hkUserAlbumContent);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkUserAlbum hkUserAlbum) {
		super.delete(hkUserAlbum);
		hkUserAlbumContentDao.delete(new HkUserAlbumContent(hkUserAlbum));
	}
	
}